package cc.fine.svc.main.aspect;

import cc.fine.svc.config.redis.utils.RedisCache;
import cc.fine.svc.main.bean.VideoOpCount;
import cc.fine.svc.main.service.VideoCountService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @AUTHOR: wyihe
 * @DATE: 2023/10/28
 * <p>
 * 如果在增加favor、common等等，redis中没有bean时，添加默认Bean
 */
@Component
@Aspect
@Slf4j
public class VideoCountRedisRespect {

    @Pointcut("execution(* cc.fine.svc.main.service.impl.VideoCountServiceImpl.*(..))")
    public void pc() {
    }


    @Autowired
    private RedisCache redisCache;

    @Before("pc()")
    public void before(JoinPoint joinPoint) {
        String videoId = (String) joinPoint.getArgs()[0];
        Object cacheMapValue = redisCache.getCacheMapValue(VideoCountService.VIDEO_COUNT_MAP_CACHE, videoId);
        if (cacheMapValue == null) {
            redisCache.setCacheMapValue(VideoCountService.VIDEO_COUNT_MAP_CACHE, videoId, new VideoOpCount());
        }
    }

}
